package pers.lance.mvcrest.plus.plus.dao.impl;

import org.springframework.stereotype.Repository;
import pers.lance.mvcrest.plus.plus.dao.BookDao;
import pers.lance.mvcrest.plus.plus.pojo.Book;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

/**
 * @ Author     ：LILA3
 * @ Date       ：Created in 6:40 PM 7/26/2018
 */
@Repository
public class BookDaoImpl implements BookDao {

    @PersistenceContext(name = "em")
    private EntityManager em;

    @Override
    public Book insert(Book book) {
        em.persist(book);
        return book;
    }

    @Override
    public Book update(Book book) {
        em.merge(book);
        return book;
    }

    @Override
    public Book delete(Book book) {
        em.remove(book);
        return book;
    }

    @Override
    public Book findById(String id) {
        Book book = em.getReference(Book.class, id);
        return book;
    }

    @Override
    public List<Book> findAll() {
        String sql = "from Book";
        return em
                .createQuery(sql)
                .getResultList();
    }
}
